﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BooksOnline.Serivces;

namespace BooksOnline.Controllers
{
    public class ReferencesController : Controller
    {
        //
        // GET: /References/

        public PartialViewResult ListMyReferences()
        {
            var booksModel = ModelFactory.GetBooksModel();
            var userId = Convert.ToInt32(System.Web.HttpContext.Current.Session["UserId"]);
            IEnumerable<UserFavouriteReferences> userFavouriteReferences =
                from books in booksModel.UserFavouriteReferences
                where books.User.Id == userId
                select books;

            ViewData.Model = userFavouriteReferences;

            return PartialView();
        }

        public ActionResult ListAllReferences()
        {
            var booksModel = ModelFactory.GetBooksModel();
            IEnumerable<Reference> references = booksModel.References;

            ViewData.Model = references;

            return View();
        }

        //
        // GET: /References/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /References/Create

        public ActionResult Create()
        {
            var bookReferences = ModelFactory.GetBooksModel();

            ViewBag.Books = bookReferences.Books.ToList();

            return View();
        } 

        //
        // POST: /References/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                var model = ModelFactory.GetBooksModel();
                var reference = new Reference();
                var bookId = Convert.ToInt32(collection["Books"]);
                var isCached = Convert.ToBoolean(collection["IsCached"]);

                reference.Name = collection["Name"];
                reference.Type = collection["Type"];
                reference.Book =
                    (from book in model.Books
                    where book.Id == bookId
                    select book).FirstOrDefault();
                reference.IsCached = isCached;
                
                model.AddToReferences(reference);

                UnitOfWork.CurrentInstance.Commit();

                return RedirectToAction("ListAllReferences");
            }
            catch(Exception ex)
            {
                return View();
            }
        }
        
        //
        // GET: /References/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /References/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /References/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /References/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
